<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('字段名')" prop="fieldName">
            <el-input v-model="form.fieldName" :disabled="form.id" :placeholder="$t('字段名')" show-word-limit maxlength="32" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('字段别名')" prop="fieldAlias">
            <el-input v-model="form.fieldAlias" :placeholder="$t('字段别名')" show-word-limit maxlength="32" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('字段类型')" prop="fieldType">
            <dictSelect
              v-model="form.fieldType"
              :clearable="false"
              dictionary="key_word_field_type"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('格式')" prop="format">
            <el-input v-model="form.format" :placeholder="$t('格式')" show-word-limit maxlength="20" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('字段长度')" prop="fieldLength">
            <el-input-number v-model="form.fieldLength" controls-position="right" :min="0" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="system:word" />
    </div>
  </div>
</template>

<script>
    import { saveWordOnlineKeyAlias, getWordOnlineKeyAlias } from '@/api/system/word/wordOnlineKeyAlias'
    export default {
        data() {
            return {
                // 表单参数
                form: {},
                // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                  fieldName: [
                    { required: true, message: this.$t('必填信息'), trigger: 'change' }
                  ],
                  fieldAlias: [
                    { required: true, message: this.$t('必填信息'), trigger: 'change' }
                  ],
                  fieldLength: [
                    { required: true, message: this.$t('必填信息'), trigger: 'change' }
                  ],
                  fieldType: [
                    { required: true, message: this.$t('必填信息'), trigger: 'change' }
                  ]
                }
            }
        },
        created() {
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getWordOnlineKeyAlias(data.id).then(response => {
                        this.form = response.data
                        this.loading = false
                    }).catch(() => {
                      this.loading = false
                    })
                } else {
                    this.reset()
                    this.loading = false
                }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    fieldName: undefined,
                    fieldAlias: undefined,
                    createBy: undefined,
                    updateBy: undefined,
                    createTime: undefined,
                    updateTime: undefined,
                    delFlag: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                  this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveWordOnlineKeyAlias(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                                this.loading = false
                            }
                            this.loading = false
                        })
                    } else {
                       this.locationError(document)
                       this.loading = false
                    }
                }).catch(() => {
                    this.loading = false
                })
           }
        }
    }
</script>
